namespace ScrumSprintMonitor.TestbenchApp
{
	#region #using Directives

	using System;
	using System.Diagnostics.CodeAnalysis;
	using System.Linq;
	using System.Windows;

	using Ninject.Core;

	using Rhino.Mocks;

	using ScrumSprintMonitor.Design;
	using ScrumSprintMonitor.Infrastructure;
	using ScrumSprintMonitor.Infrastructure.Configuration;
	using ScrumSprintMonitor.UI.Wpf;

	using Testbench.Impl;

	using Views.TestbenchView;

	#endregion

	internal class NinjectAppDesignTimeModule : StandardModule
	{
		/// <summary>
		/// Loads the module into the kernel.
		/// </summary>
		public override void Load()
		{
			SetupDesignTimeBindings();
		}

		private void SetupDesignTimeBindings()
		{
			var testbenchViewModel = MockRepository.GenerateStub<ITestbenchViewModel>();
			testbenchViewModel.Stub(x => x.AvailableBuildStatus).Return(Enum.GetValues(typeof(BuildStatus)).Cast<BuildStatus>().ToList());
			testbenchViewModel.CurrentBuildStatus = BuildStatus.NotSetUp;
			testbenchViewModel.Stub(x => x.WorkItems).Return(WritableWorkItem.ConvertToBindingList(TestAssetLibrary.CreateWorkItems()));
			testbenchViewModel.Stub(x => x.ValidTeamMembers).Return(TestAssetLibrary.CreateTeamMemberInfoProvider().TeamMembers.Select(x => x.Name).ToList());
			Bind<ITestbenchViewModel>().ToConstant(testbenchViewModel);

			var shellLayoutConfiguration = new ShellLayoutConfiguration(false, true, SprintMode.Sprinting);
			Bind<IShellLayoutConfiguration>().ToConstant(shellLayoutConfiguration);
		}
	}

	/// <summary>
	/// Provides a custom implementation of DesignerProperties.GetIsInDesignMode
	/// to work around an issue.
	/// </summary>
	internal static class StubDesignerProperties
	{
		/// <summary>
		/// Stores the computed InDesignMode value.
		/// </summary>
		private static bool? isInDesignMode;

		/// <summary>
		/// Returns whether the control is in design mode (running under Blend
		/// or Visual Studio).
		/// </summary>
		/// <param name="element">The element from which the property value is
		/// read.</param>
		/// <returns>True if in design mode.</returns>
		[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "element",
			Justification = "Matching declaration of System.ComponentModel.DesignerProperties.GetIsInDesignMode (which has a bug and is not reliable).")]
		public static bool GetIsInDesignMode(DependencyObject element)
		{
			if (!isInDesignMode.HasValue)
			{
				isInDesignMode = (null == Application.Current) || !(Application.Current is App);
			}
			return isInDesignMode.Value;
		}
	}
}